package com.icebergteam.warfacewidget.ui.configuration;

import com.icebergteam.warfacewidget.AppManager;
import com.icebergteam.warfacewidget.core.LocaleManager;
import com.icebergteam.warfacewidget.core.LogManager;
import com.icebergteam.warfacewidget.core.settings.FinderSettings;
import com.icebergteam.warfacewidget.R;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class PrefsFinderFragment extends Fragment implements PrefsFragment {
	public static final String className = PrefsFinderFragment.class.getCanonicalName();
	
	public static final int FRAGMENT_ID = 1;
	
	private FinderSettings	finderSettings;
	private LocaleManager	localeManager;
	private LinearLayout	prefsContainer;
	// Active views
	private CheckBox	showAuticompleteList;
	private CheckBox	useCurrentRegion;
	private TextView	resultItemsCount;
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		try {
			finderSettings	= AppManager.getFinderSettings();
			localeManager	= AppManager.getLocaleManager();
		} catch (Exception e) {
			LogManager.LogE(className, LogManager.getExecuteMethodName(), e.getMessage());
		}
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		ScrollView mainView = (ScrollView)inflater.inflate(R.layout.prefs_template_layout, container, false);
		
		prefsContainer = (LinearLayout) mainView.findViewById(R.id.prefs_main_layout);
		prefsContainer.addView(inflater.inflate(R.layout.prefs_finder_request_layout,	prefsContainer, false));
		prefsContainer.addView(inflater.inflate(R.layout.prefs_finder_response_layout,	prefsContainer, false));
		
		setViews();
		try {
			setValues();
		} catch (Exception e) {
			LogManager.LogE(className, LogManager.getExecuteMethodName(), e.getMessage());
		}
				
		return mainView;
	}

	@Override
	public void setViews() {
		showAuticompleteList	= (CheckBox) prefsContainer.findViewById(R.id.prefs_finder_request_show_autocomplete_list_checkBox);
		useCurrentRegion		= (CheckBox) prefsContainer.findViewById(R.id.prefs_finder_request_use_current_region_checkBox);
		resultItemsCount		= (TextView) prefsContainer.findViewById(R.id.prefs_finder_response_result_items_count_textView);
	}

	@Override
	public void setValues() throws Exception {
		showAuticompleteList.setChecked(finderSettings.isShowAuticompleteList());
		useCurrentRegion.setChecked(finderSettings.isAlwaysUseCurrentRegion());
		String currentResultItemsCount = String.format("%d", finderSettings.getResultItemsCount());
			//	+ " " + localeManager.getLocalizedHours(finderSettings.getResultItemsCount()); // TODO: Make universal
		resultItemsCount.setText(currentResultItemsCount);
		
		finderSettings.save();
	}

	@Override
	public boolean checkFields(View v) throws Exception {
		ConfigurationActivity.startTapFieldAnimation(v);
		switch(v.getId())	{
		// Show autocomplete list
		case R.id.prefs_finder_request_show_autocomplete_list_field:
			setShowAutocompleteList();
			break;
		// Use current region
		case R.id.prefs_finder_request_use_current_region_field:
			setUseCurrentRegion();
			break;
		// Result items count
		case R.id.prefs_finder_response_result_items_count_field:
			showSelectResultItemsCountDialog();
			break;
		default:
			return false;
		}
		setValues();
		return true;
	}

	private void setShowAutocompleteList() {
		finderSettings.setShowAuticompleteList(!finderSettings.isShowAuticompleteList());
	}
	
	private void setUseCurrentRegion() {
		finderSettings.setAlwaysUseCurrentRegion(!finderSettings.isAlwaysUseCurrentRegion());
	}
	
	private void showSelectResultItemsCountDialog() throws Exception {
		 View updatePeriodLayout = getResultItemsCountLayout(); // TODO: Make this method in class
		 
			
		AlertDialog.Builder ad = new AlertDialog.Builder(getActivity())
			.setTitle(R.string.prefs_finder_group_response_result_items_count_desc)
			.setCancelable(true)
		// Add items
			.setView(updatePeriodLayout)
		// Add buttons
			.setNegativeButton(R.string.dialog_cancel, new OnClickListener()	{
		
				@Override
				public void onClick(DialogInterface dialog, int which) {
					try {
						setValues();
						dialog.cancel();
					} catch (Exception e) {
						LogManager.LogE(className, LogManager.getExecuteMethodName(), e.getMessage());
						e.printStackTrace();
					}
				}
			})
			.setPositiveButton(R.string.dialog_ok, new OnClickListener()	{

				@Override
				public void onClick(DialogInterface dialog, int which) {
					try {
						SeekBar updatePeriodSeekBar = (SeekBar) ((AlertDialog) dialog).findViewById(R.id.prefs_news_autoupate_period_dialog_seekbar);
						finderSettings.setResultItemsCount((updatePeriodSeekBar.getProgress() + finderSettings.getMinResultItemsCount()));
						setValues();
						dialog.cancel();
					} catch (Exception e) {
						LogManager.LogE(className, LogManager.getExecuteMethodName(), e.getMessage());
						e.printStackTrace();
					}					
				}
			});
		// Show
		ad.show();
	}			

	// Result items count
	private View getResultItemsCountLayout() throws Exception	{
		// Setup layout
		LayoutInflater li = LayoutInflater.from(getActivity());
		View v = li.inflate(R.layout.prefs_seekbar_dialog_layout, null);
		// Setup seekbar
		SeekBar updatePeriodSeekBar = (SeekBar) v.findViewById(R.id.prefs_news_autoupate_period_dialog_seekbar);
		updatePeriodSeekBar.setMax(finderSettings.getMaxResultItemsCount() - finderSettings.getMinResultItemsCount());
		updatePeriodSeekBar.setProgress(finderSettings.getResultItemsCount() - finderSettings.getMinResultItemsCount());
		// Setup text views
			// Setup current value
		TextView currentValue = (TextView) v.findViewById(R.id.prefs_seekbar_current_value_textView);
		String updatePeriod = String.format("%d", finderSettings.getResultItemsCount());
				// + " " + localeManager.getLocalizedHours(newsSettings.getRSSUpdatePeriod()); // TODO: Make it universal
		currentValue.setText(updatePeriod);	
			// Setup min value
		TextView minValue = (TextView) v.findViewById(R.id.prefs_seekbar_min_value_textView);
		minValue.setText(String.format("%d", finderSettings.getMinResultItemsCount()));
			// Setup max value
		TextView maxValue = (TextView) v.findViewById(R.id.prefs_seekbar_max_value_textView);
		maxValue.setText(String.format("%d", finderSettings.getMaxResultItemsCount()));
		// Setup seekbar listener
		updatePeriodSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
	
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				View v = (View) seekBar.getParent();
				TextView currentValue = (TextView) v.findViewById(R.id.prefs_seekbar_current_value_textView);
				
				try {
					String updatePeriod = String.format("%d", progress + finderSettings.getMinResultItemsCount());
							// + " " + localeManager.getLocalizedHours(progress + newsSettings.getMinUpdatePeriod()); // TODO: Make it universal
					currentValue.setText(updatePeriod);	
				} catch (Exception e) {
					LogManager.LogE(className, LogManager.getExecuteMethodName(), e.getMessage());
					e.printStackTrace();
				}
					
			}
	
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// Do nothing
			}
	
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				// Do nothing		
			}
		});
		
		return v;
	}
}
